﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.Entity.EntityDto.System.Code;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.System.Code;
using VM.IBLL.Topic;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace VM.WebAPI.Controllers
{


    /// <summary>
    /// 数据字典相关控制器
    /// </summary>
    [Route("api/codeHelper")]
    [ApiController]
    public class CodeHelperController : ControllerBase
    {
        private readonly ICodeHelperBLL _ICodeHelperBLL;
        private readonly IJWTService _IJWTService;
        private readonly IHttpContextAccessor _HttpContextAccessor;

        public CodeHelperController(IJWTService iJWTService, ICodeHelperBLL iCodeHelperBLL, IHttpContextAccessor httpContextAccessor)
        {

            _ICodeHelperBLL = iCodeHelperBLL;
            _IJWTService = iJWTService;
            _HttpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 添加编码规则
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addCodeRule")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addCodeRule(addCodeRuleDto obj)
        { 
            //解析Jwt,获取到UserId
            int UserId= _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj= _ICodeHelperBLL.perfectClass<addCodeRuleDto>(obj,UserId,ProTypeEnum.save);
            return _ICodeHelperBLL.insertCodeRule(obj);
        } 
        /// <summary>
        /// 更新编码规则
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("updateCodeRule")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult updateCodeHelper(updateCodeRuleDto obj)
        {
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj = _ICodeHelperBLL.perfectClass<updateCodeRuleDto>(obj, UserId, ProTypeEnum.update);
            return _ICodeHelperBLL.updateCodeRule(obj);
        }
      
        /// <summary>
        /// 获取编码规则列表，   PC
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getCodeRuleList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<PagingData<getCodeRuleListDto>> getDataBaseTypeList(string? name = "", int pageIndex = 1, int pageSize = 100)
        {
            //获取到header 内的用户信息进行解析
            var authorizationHeader = _HttpContextAccessor.HttpContext.Request.Headers["Authorization"];
            //解析jwt
            string tokenInfo = authorizationHeader.ToString().Replace("bearer ", "").Replace("Bearer ", "");
            var AESId = _IJWTService.AnaliseToken(tokenInfo, "id");
            //解析userid
            int UserId = Convert.ToInt32(AESUntil.Decrypt(AESId));
            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<PagingData<getCodeRuleListDto>> Res = _ICodeHelperBLL.getCodeRuleList(name, UserId, pageSize, pageIndex);
            return Res;
        }


        /// <summary>
        /// 添加编码
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addCode")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addCode(addCodeRuleDto obj)
        {
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj = _ICodeHelperBLL.perfectClass<addCodeRuleDto>(obj, UserId, ProTypeEnum.save);
            return _ICodeHelperBLL.insertCodeRule(obj);
        }

        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("getNextCode")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult getNextCode(int menuId)
        {
            return null;
        }
    }
}
